﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.SharePoint;

namespace UnitTest
{
    [TestClass]
    public class ReadEvent
    {
        [TestInitialize]
        public void Init()
        {
            this.site = new SPSite("http://xchai1");
            this.web = site.OpenWeb();
            this.calendar = web.GetList("/Lists/Calendar");

            if (this.web.CurrentUser != null
                && this.web.CurrentUser.RegionalSettings != null)
                this.regionalSettings = this.web.CurrentUser.RegionalSettings;
            else
                this.regionalSettings = this.web.RegionalSettings;

        }

        [TestCleanup]
        public void Cleanup()
        {
            this.web.Dispose();
            this.site.Dispose();
        }

        SPSite site;
        SPWeb web;
        SPList calendar;
        SPRegionalSettings regionalSettings;

        [TestMethod]
        public void ReadSimpleEvent()
        {
            var item = this.calendar.GetItemById(4);
            var evt = WindStyle.SPCalendar.Event.ParseEvent(item, this.regionalSettings);

            Assert.AreEqual(item.Title, 
                evt.Title);

            Assert.AreEqual(item[SPBuiltInFieldId.Location] as string, 
                evt.Location);

            Assert.AreEqual((DateTime)item[SPBuiltInFieldId.StartDate],
                evt.Start);

            Assert.AreEqual(item[SPBuiltInFieldId.fAllDayEvent] != null && bool.Parse(item[SPBuiltInFieldId.fAllDayEvent].ToString()),
                evt.IsAllDayEvent);

            Assert.IsFalse(evt.IsRecurrence);

            Assert.AreEqual(item[SPBuiltInFieldId.Category],
                evt.Category);

            Assert.AreEqual(item[SPBuiltInFieldId.Workspace],
                evt.Workspace);
        }

        [TestMethod]
        public void ReadDailyByFrequencyEvent()
        {
            var item = this.calendar.GetItemById(5);
            var evt = WindStyle.SPCalendar.Event.ParseEvent(item, this.regionalSettings);

            Assert.AreEqual(WindStyle.SPCalendar.EventType.DailyByFrequency,
                evt.Type);

            var realEvent = evt as WindStyle.SPCalendar.Event<WindStyle.SPCalendar.Pattern.DailyByFrequencyPattern>;

            Assert.IsTrue(realEvent.IsRecurrence);

            Assert.IsNotNull(realEvent.Recurrence);

            Assert.AreEqual(((DateTime)item[SPBuiltInFieldId.StartDate]).TimeOfDay,
                realEvent.Start.TimeOfDay);

            Assert.AreEqual(((DateTime)item[SPBuiltInFieldId.StartDate]).Date,
                realEvent.Recurrence.DateRange.StartDate.Date);

            Assert.AreEqual(2,
                realEvent.Recurrence.Pattern.Frequency);

            Assert.AreEqual(WindStyle.SPCalendar.EndType.NoEnd,
                realEvent.Recurrence.DateRange.End.Type);
        }

        [TestMethod]
        public void ReadDailyByWeekdayEvent()
        {
            var item = this.calendar.GetItemById(6);
            var evt = WindStyle.SPCalendar.Event.ParseEvent(item, this.regionalSettings);

            Assert.AreEqual(WindStyle.SPCalendar.EventType.DailyByWeekday,
                evt.Type);

            var realEvent = evt as WindStyle.SPCalendar.Event<WindStyle.SPCalendar.Pattern.DailyByWeekdayPattern>;

            Assert.IsTrue(realEvent.IsRecurrence);

            Assert.IsNotNull(realEvent.Recurrence);

            Assert.AreEqual(WindStyle.SPCalendar.EndType.ByInstances,
                realEvent.Recurrence.DateRange.End.Type);

            Assert.AreEqual(10,
                realEvent.Recurrence.DateRange.End.Instances);
        }

        [TestMethod]
        public void ReadWeeklyEvent()
        {
            var item = this.calendar.GetItemById(7);
            var evt = WindStyle.SPCalendar.Event.ParseEvent(item, this.regionalSettings);

            Assert.AreEqual(WindStyle.SPCalendar.EventType.Weekly,
                evt.Type);

            var realEvent = evt as WindStyle.SPCalendar.Event<WindStyle.SPCalendar.Pattern.WeeklyPattern>;

            Assert.IsTrue(realEvent.IsRecurrence);

            Assert.IsNotNull(realEvent.Recurrence);

            Assert.AreEqual(2,
                realEvent.Recurrence.Pattern.Frequency);

            Assert.AreEqual(WindStyle.SPCalendar.DaysOfWeek.Monday,
                            realEvent.Recurrence.Pattern.Days & WindStyle.SPCalendar.DaysOfWeek.Monday);

            Assert.AreEqual(WindStyle.SPCalendar.DaysOfWeek.Friday,
                            realEvent.Recurrence.Pattern.Days & WindStyle.SPCalendar.DaysOfWeek.Friday);

            Assert.AreNotEqual(WindStyle.SPCalendar.DaysOfWeek.Sunday,
                            realEvent.Recurrence.Pattern.Days & WindStyle.SPCalendar.DaysOfWeek.Sunday);
            
            Assert.AreEqual(WindStyle.SPCalendar.EndType.ByDate,
                realEvent.Recurrence.DateRange.End.Type);

            Assert.AreEqual(new DateTime(2013, 8, 31),
                realEvent.Recurrence.DateRange.End.Date.Date);
        }

        [TestMethod]
        public void ReadMonthlyByFrequencyEvent()
        {
            var item = this.calendar.GetItemById(8);
            var evt = WindStyle.SPCalendar.Event.ParseEvent(item, this.regionalSettings);

            Assert.AreEqual(WindStyle.SPCalendar.EventType.MonthlyByFrequency,
                evt.Type);

            var realEvent = evt as WindStyle.SPCalendar.Event<WindStyle.SPCalendar.Pattern.MonthlyByFrequencyPattern>;

            Assert.IsTrue(realEvent.IsRecurrence);

            Assert.IsNotNull(realEvent.Recurrence);

            Assert.AreEqual(2, 
                realEvent.Recurrence.Pattern.MonthFrequency);

            Assert.AreEqual(24, 
                realEvent.Recurrence.Pattern.Day);
        }

        [TestMethod]
        public void ReadMonthlyByDayEvent()
        {
            var item = this.calendar.GetItemById(9);
            var evt = WindStyle.SPCalendar.Event.ParseEvent(item, this.regionalSettings);

            Assert.AreEqual(WindStyle.SPCalendar.EventType.MonthlyByDay,
                evt.Type);

            var realEvent = evt as WindStyle.SPCalendar.Event<WindStyle.SPCalendar.Pattern.MonthlyByDayPattern>;

            Assert.IsTrue(realEvent.IsRecurrence);

            Assert.IsNotNull(realEvent.Recurrence);

            Assert.AreEqual(2, 
                realEvent.Recurrence.Pattern.MonthFrequency);

            Assert.AreEqual(WindStyle.SPCalendar.Ordinal.Third,
                realEvent.Recurrence.Pattern.Ordinal);

            Assert.AreEqual(WindStyle.SPCalendar.DaysOfWeek.WeekendDay,
                realEvent.Recurrence.Pattern.Day);
        }

        [TestMethod]
        public void ReadYearlyByFrequencyEvent()
        {
            var item = this.calendar.GetItemById(10);
            var evt = WindStyle.SPCalendar.Event.ParseEvent(item, this.regionalSettings);

            Assert.AreEqual(WindStyle.SPCalendar.EventType.YearlyByFrequency,
                evt.Type);

            var realEvent = evt as WindStyle.SPCalendar.Event<WindStyle.SPCalendar.Pattern.YearlyByFrequencyPattern>;

            Assert.IsTrue(realEvent.IsRecurrence);

            Assert.IsNotNull(realEvent.Recurrence);

            Assert.AreEqual(6,
                realEvent.Recurrence.Pattern.Month);

            Assert.AreEqual(25, 
                realEvent.Recurrence.Pattern.Day);

        }

        [TestMethod]
        public void ReadYearlyByDayEvent()
        {
            var item = this.calendar.GetItemById(11);
            var evt = WindStyle.SPCalendar.Event.ParseEvent(item, this.regionalSettings);

            Assert.AreEqual(WindStyle.SPCalendar.EventType.YearlyByDay,
                evt.Type);

            var realEvent = evt as WindStyle.SPCalendar.Event<WindStyle.SPCalendar.Pattern.YearlyByDayPattern>;

            Assert.IsTrue(realEvent.IsRecurrence);

            Assert.IsNotNull(realEvent.Recurrence);

            Assert.AreEqual(6, realEvent.Recurrence.Pattern.Month);

            Assert.AreEqual(WindStyle.SPCalendar.Ordinal.Third,
                realEvent.Recurrence.Pattern.Ordinal);

            Assert.AreEqual(WindStyle.SPCalendar.DaysOfWeek.Friday,
                realEvent.Recurrence.Pattern.Days);
        }
    }
}
